package org.fixies.spreadsheet;

import org.fixies.utils.Validator;

import java.util.Collections;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;

/**
 * メタデータ
 *
 * @author Kawasaki
 */
public class MetaData {

    private final Map<String, String> metaData_;

    public MetaData(Map<String, String> metaData) {
        Validator.notNull(metaData);
        metaData_ = Collections.unmodifiableMap(metaData);
    }

    public MetaData(Map<String, String>... metaData) {
        Validator.notNullOrEmpty(metaData);
        Map<String, String> m = metaData[0];
        for (int i = 1; i < metaData.length; i++) {
            m.putAll(metaData[i]);
        }
        metaData_ = m;
    }

    public String getMetaData(String key) {
        return metaData_.get(key);
    }

    public String getRequiredMetaData(String key) {
        String value = getMetaData(key);
        if (Validator.isNullOrEmpty(value)) {
            throw new NoSuchElementException("key=[" + key + "], but was " + toString());
        }
        return value;
    }

    @Override
    public String toString() {
        return "METADATA=[" + metaData_.toString() + "]";
    }

    public Set<String> getMetaDataKeys() {
        return metaData_.keySet();
    }

    public boolean contains(String key) {
        return metaData_.containsKey(key);
    }
}
